
import Core.SlaveInfo;
import Message.GetSlaveResultMessage;
import Message.GetSlaveResultMessageHandler;
import Core.MessageBuilder;
import Core.MessageType;
import Core.IChat;
import Core.IMessageProvider;
import Core.IMessageHandler;
import java.io.DataInputStream;
import java.io.PrintStream;
import java.util.Vector;
import javax.microedition.io.Connector;
import javax.microedition.io.SocketConnection;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Mr Bao
 */
public class MasterClient {

    private String host;
    private int port;
    private SocketConnection socket;
    private DataInputStream in;
    private PrintStream out;
    private static IMessageProvider messageProvider;
    private boolean isGoing = false;
    private Vector queueMessage;
    private SlaveInfo slaveInfo;

    public MasterClient(String host, int port, Vector queueMessage) {
        this.host = host;
        this.port = port;
        this.queueMessage = queueMessage;
    }

    static {
        Boot();
    }

    static void Boot() {
        messageProvider = new MessageProvider();
        messageProvider.Register(GetSlaveResultMessage.class, new GetSlaveResultMessageHandler());
    }

    public boolean GetSlaveServer() {
        try {
            socket = (SocketConnection) Connector.open("socket://" + host + ":" + port);
            in = new DataInputStream(socket.openInputStream());
            out = new PrintStream(socket.openOutputStream());
            String message = MessageBuilder.BuildGetSlaveMessage();
            out.println(message);
            out.flush();
            StringBuffer buffer = new StringBuffer();
            char c;
            while ((c = in.readChar()) != -1 && c != '\n') {
                buffer.append(c);
            }
            String reply = buffer.toString();
            if (reply == null || reply.trim().length() == 0) {
                return false;
            }
            if (reply.startsWith(MessageType.GetSlaveResultMessage)) {
                IMessageHandler messageHandler = messageProvider.GetMessageHandler(GetSlaveResultMessage.class);
                slaveInfo = (SlaveInfo) messageHandler.Process(reply);
                String ok = MessageBuilder.BuildHaveGotSlaveMessage();
                StopConnectWithMasterServer();
            }
            buffer.delete(0, buffer.capacity());
            buffer = null;
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    private void StopConnectWithMasterServer() {
        if (socket != null) {
            try {
                socket.close();
                socket = null;
                if (out != null) {
                    out.close();
                    out = null;
                }
                if (in != null) {
                    in.close();
                    in = null;
                }
            } catch (Exception e) {
            }
        }
        Runtime.getRuntime().gc();
    }

    private void StartConnectWithSlaveServer(String address, int port) {
        Runtime.getRuntime().gc();
    }
}
